<template>
	<div class="left-box">
		<div class="heade-wrapper">
			<a-input-search
				class="last-input-box"
				:placeholder="this.$i18n('selectPeople.input',this.$i18n('org.team.label'))"
				v-model.trim="searchValue"
				@search="searchHandle"
			/>
		</div>
		<top-bottom-layout class="content-wrapper content-wrapper-box">
			<template #top>
				<scroll-list
					panel="Team"
					command="teamList"
					ref="teamList"
					:showCheckbox="showCheckbox"
					:initSearchValue="teamSearchCondition"
					:singleAndMult="true"
					:checkAllShow="false"
					:checkBoxStrictly="true"
					:otherProps="teamOtherProps"
					:titleProps="teamTitleProps"
					iconType="cap-icon-alternative-group"
					@toCheckValueClickProp="getTeamValueClickPropHandle"
					@tocheckBoxChange="oncheckBoxChange"
				/>
			</template>
			<template #middle>
				<a-checkbox
					:checked="relatedPeople"
					class="content-center-checkbox"
					@change="showRelatedPeopleHandle"
				>
					{{ $i18n('org.team.showRelative.label') }}
				</a-checkbox>
			</template>
			<template #bottom>
				<scroll-list
					panel="Team"
					command="memberList"
					ref="peopleList"
					:otherProps="peopleOtherProps"
					:titleProps="peopleTitleProps"
					:firstLoadSearch="peopleFirstLoad"
				/>
			</template>
		</top-bottom-layout>
	</div>
</template>

<script>
import scrollList from '@/components/scrollList';
import topBottomLayout from '@/components/topBottomLayout';
import { CheckUtils } from '@/utils/checkUtils';
import CtpI18n from '@/utils/ctpI18n';
export default {
	name: 'Team',
	components: {
		scrollList,
		topBottomLayout
	},
	inject: ['app'],
	data() {
		return {
			searchValue: '',
			selectAccountId: '',
			teamId: '',
			relatedPeople: false,
			peopleFirstLoad: false,
			teamSearchCondition: {},
			// peopleOtherProps: ['unitName', 'teamMemberName'],
			peopleTitleProps: { teamMemberName: this.$i18n('edoc.form.sort'), unitName: this.$i18n('org.account.label') },
			teamOtherProps: ['typeName'],
			teamTitleProps: { typeName: this.$i18n('selectPeople.main.type') }
		};
	},
	computed: {
		showCheckbox() {
			return this.app().orgSelect.selectType.includes('Team');
		},
		orgSelect() {
			return this.app().orgSelect;
		},
		peopleOtherProps() {
			return this.onSetOtherProps(['unitName', 'teamMemberName']);
		}
	},
	created() {
		if (this.app().switchAfterUnit.id) {
			this.selectAccountId = this.app().switchAfterUnit.id;
		} else {
			this.selectAccountId = this.orgSelect.defaultAccountId;
		}
		this.teamSearchCondition = {
			accountId: this.selectAccountId,
			showTeamType: this.app().orgSelect.showTeamType
		};
	},
	methods: {
		initProps() {
			this.searchValue = '';
			this.teamId = '';
			this.relatedPeople = false;
			this.peopleFirstLoad = false;
		},
		onChangeAccount(ret) {
			this.initProps();
			this.$refs.peopleList.initProps(); // 人员清空
			this.selectAccountId = ret.id;
			this.searchHandle();
		},
		searchHandle() {
			this.$refs.peopleList.initProps();
			this.$refs.teamList.search({
				accountId: this.selectAccountId,
				keyword: this.searchValue,
				showTeamType: this.app().orgSelect.showTeamType
			});
		},
		getTeamValueClickPropHandle(value) {
			this.teamId = value.id;
			this.peopleSearchHandle();
		},
		showRelatedPeopleHandle(e) {
			this.relatedPeople = e.target.checked;
			if (!this.teamId) {
				return;
			}
			this.peopleSearchHandle();
		},
		peopleSearchHandle() {
			let { accessFileSecretLevel, includeElements, excludeElements } =
				this.orgSelect;
			this.peopleFirstLoad = true;
			this.$refs.peopleList.search({
				teamId: this.teamId,
				accountId: this.selectAccountId,
				isShowRelatemember: String(this.relatedPeople),
				accessFileSecretLevel,
				includeElements,
				excludeElements
			});
		},
		/**
		 * 判断组中有没有人员，再根据参数判断能否选择空组
		 * unallowedSelectEmptyGroup： true|false 不允许选择空的部门, 默认false 允许选择空的部门
		 * isNotShowNoMemberConfirm：  true|false 是否不显示“xxx部门下无人，是否继续选择”的提示语，默认false，即显示
		 */
		oncheckBoxChange(node) {
			let nodeList = node;
			if (!(node instanceof Array)) {
				nodeList = [node];
			}
			this.onCheckScopeStatus(nodeList);
		},
		async onCheckScopeStatus(nodes) {
			let entity = nodes[0];
			let notIncludeSubDept = [];
			let checkContext = {
				currentPanel: 'Team',
				notIncludeSubDept
			};
			let result = await CheckUtils.doCheckEntity(entity, checkContext);
			if (!result) {
				return;
			}
			this.selectDataToRight(nodes);
		},
		/**
		 * 人员【密级】展示处理
		 * @param {Array} list 默认展示的人员信息
		*/
		onSetOtherProps(list) {
			if (this.orgSelect.showSecretLevel) {
				list.unshift('secretLevel')
			}
			return list
		}
	}
};
</script>

<style lang="scss">
.panel-box {
	.middle-box{
		padding-top: 0px;
	}
}
</style>
